﻿using System;
using System.Linq;
using ClusterAware;
using System.Management;
using System.Threading;
using ClusterAwareAdmin.Properties;
using ClusterAwareProperties;

namespace ClusterAwareAdmin
{
    /// <summary>
    /// Admin Node Methods Class
    /// </summary>
    public class NodeAdmin : NodeMethods
    {
        #region private properties

        private readonly ClusterManagerAdmin _managerclass;

        #endregion

        #region CTOR

        /// <summary>
        /// Create Class NodeMethodsAdmin
        /// </summary>
        /// <param name="managerclass">Instance of ClusterManager class</param>
        public NodeAdmin(ClusterManagerAdmin managerclass)
            : base(managerclass.ManagerInstance)
        {
            _managerclass = managerclass;
        }

        #endregion

        #region public Methods

        ///<summary>
        /// Pause a Node
        ///</summary>
        ///<param name="nodeName">node Name</param>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter Node name is empty, 
        /// Node not exists, 
        /// Failed WMI method execution
        /// </exception>
        public void Pause(string nodeName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (_managerclass.OperationSystem == WindowsOperationSystem.Windows2003 || _managerclass.OperationSystem == WindowsOperationSystem.Windows2003R2)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_SystemNotSuportMethod, _managerclass.OperationSystem, "Pause"));
            }
            if (string.IsNullOrEmpty(nodeName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "nodeName"));
            }
            if (!_managerclass.Nodes.ExistNode(nodeName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Node", nodeName));
            }
            try
            {
                var node = _managerclass.Nodes.GetNode(nodeName);
                using (var inParams = node.Instance.GetMethodParameters("Pause"))
                {
                    node.Instance.InvokeMethod("Pause", inParams, null);
                    if (_managerclass.CacheInstances)
                    {
                        _managerclass.Refresh(RefreshOption.Nodes,nodeName);
                    }
                }
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Nodes.Pause", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Nodes.Pause", ex.Message), ex);
            }
        }


        ///<summary>
        /// Resume a Node
        ///</summary>
        ///<param name="nodeName">node name</param>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter Node name is empty, 
        /// Node not exists, 
        /// Failed WMI method execution
        /// </exception>
        public void Resume(string nodeName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (_managerclass.OperationSystem == WindowsOperationSystem.Windows2003 || _managerclass.OperationSystem == WindowsOperationSystem.Windows2003R2)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_SystemNotSuportMethod, _managerclass.OperationSystem, "Resume"));
            }
            if (string.IsNullOrEmpty(nodeName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "nodeName"));
            }
            if (!_managerclass.Nodes.ExistNode(nodeName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Node", nodeName));
            }
            try
            {
                var node = _managerclass.Nodes.GetNode(nodeName);
                using (var inParams = node.Instance.GetMethodParameters("Resume"))
                {
                    node.Instance.InvokeMethod("Resume", inParams, null);
                    if (_managerclass.CacheInstances)
                    {
                        _managerclass.Refresh(RefreshOption.Nodes,nodeName);
                    }
                }
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Nodes.Resume", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Nodes.Resume", ex.Message), ex);
            }
        }

        #endregion


    }
}
